<template>
  <div class="gi_table_page">
    <GiTable
      title="航空机票订单管理"
      row-key="id"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #toolbar-left>
	    <a-input-search v-model="queryForm.orderNumber" placeholder="请输入订单编号" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.passengerName" placeholder="请输入乘客姓名" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.flightNumber" placeholder="请输入航班号" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.departureCity" placeholder="请输入出发城市" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.arrivalCity" placeholder="请输入到达城市" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.departureTime" placeholder="请输入出发时间" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.arrivalTime" placeholder="请输入到达时间" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.ticketPrice" placeholder="请输入票价" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.orderStatus" placeholder="请输入订单状态" allow-clear @search="search" />
        <DateRangePicker v-model="queryForm.createdAt" @change="search" />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['test:ticketOrders:create']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['test:ticketOrders:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['test:ticketOrders:get']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['test:ticketOrders:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['test:ticketOrders:delete']"
            status="danger"
            :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <TicketOrdersAddModal ref="TicketOrdersAddModalRef" @save-success="search" />
    <TicketOrdersDetailDrawer ref="TicketOrdersDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import type { TableInstance } from '@arco-design/web-vue'
import TicketOrdersAddModal from './TicketOrdersAddModal.vue'
import TicketOrdersDetailDrawer from './TicketOrdersDetailDrawer.vue'
import { type TicketOrdersResp, type TicketOrdersQuery, deleteTicketOrders, exportTicketOrders, listTicketOrders } from '@/apis/test/ticketOrders'
import { useDownload, useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'TicketOrders' })


const queryForm = reactive<TicketOrdersQuery>({
  orderNumber: undefined,
  passengerName: undefined,
  flightNumber: undefined,
  departureCity: undefined,
  arrivalCity: undefined,
  departureTime: undefined,
  arrivalTime: undefined,
  ticketPrice: undefined,
  orderStatus: undefined,
  createdAt: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listTicketOrders({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstance['columns'] = [
  { title: '订单ID', dataIndex: 'id', slotName: 'id' },
  { title: '订单编号', dataIndex: 'orderNumber', slotName: 'orderNumber' },
  { title: '乘客姓名', dataIndex: 'passengerName', slotName: 'passengerName' },
  { title: '航班号', dataIndex: 'flightNumber', slotName: 'flightNumber' },
  { title: '出发城市', dataIndex: 'departureCity', slotName: 'departureCity' },
  { title: '到达城市', dataIndex: 'arrivalCity', slotName: 'arrivalCity' },
  { title: '出发时间', dataIndex: 'departureTime', slotName: 'departureTime' },
  { title: '到达时间', dataIndex: 'arrivalTime', slotName: 'arrivalTime' },
  { title: '票价', dataIndex: 'ticketPrice', slotName: 'ticketPrice' },
  { title: '订单状态', dataIndex: 'orderStatus', slotName: 'orderStatus' },
  { title: '订单创建时间', dataIndex: 'createdAt', slotName: 'createdAt' },
  { title: '订单更新时间', dataIndex: 'updatedAt', slotName: 'updatedAt' },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['test:ticketOrders:get', 'test:ticketOrders:update', 'test:ticketOrders:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.orderNumber = undefined
  queryForm.passengerName = undefined
  queryForm.flightNumber = undefined
  queryForm.departureCity = undefined
  queryForm.arrivalCity = undefined
  queryForm.departureTime = undefined
  queryForm.arrivalTime = undefined
  queryForm.ticketPrice = undefined
  queryForm.orderStatus = undefined
  queryForm.createdAt = undefined
  search()
}

// 删除
const onDelete = (record: TicketOrdersResp) => {
  return handleDelete(() => deleteTicketOrders(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportTicketOrders(queryForm))
}

const TicketOrdersAddModalRef = ref<InstanceType<typeof TicketOrdersAddModal>>()
// 新增
const onAdd = () => {
  TicketOrdersAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: TicketOrdersResp) => {
  TicketOrdersAddModalRef.value?.onUpdate(record.id)
}

const TicketOrdersDetailDrawerRef = ref<InstanceType<typeof TicketOrdersDetailDrawer>>()
// 详情
const onDetail = (record: TicketOrdersResp) => {
  TicketOrdersDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
